#ifndef QMANAGERWIDGET_P_H
#define QMANAGERWIDGET_P_H

#include "qmanagerwidget.h"
#include "inputwidgetutils.h"

#include <QTableWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSpinBox>


struct DynamicFormDataItem{
    QString key;
    QString value;
    int order;
};
struct DynamicFormDataList{
    int total = 0;
    int current =0;
    int pageSize =10;
    QVector<QList<DynamicFormDataItem>> datas;
};

class QManagerWidgetPrivate : public QObject
{
    Q_DECLARE_PUBLIC(QManagerWidget)
public:
    QManagerWidgetPrivate(QManagerWidget *q);
    void initUI();
    bool initData(QSqlDatabase *_db, QTableAttribute *_tableAttribute);
private:
    void updateData();
    QSqlDatabase *db;
    QTableAttribute *tableAttribute;
    QTableWidget * tableWidget;
    QVector<QWidget *> inputWidgets;
    QPushButton *btnSearch;
    QManagerWidget *const q_ptr;
    QMap<QString,QString> maps;

    // 查询按钮
    QPushButton *m_btnQuery;
    QPushButton *m_btnPrePage;
    QPushButton *m_btnNextPage;
    QPushButton *m_btnFristPage;
    QPushButton *m_btnEndPage;
    QSpinBox *m_edtPage;
    QLabel *m_labTotalPage;
    QPushButton *m_btnJmpPage;
    int currentPage;    /* 当前页码 */
    int total;          /* 当前请求的数据的总数 */
    int totalPage;          /* 当前请求的数据的总数 */
    int size;           /* 每次请求数据的数量 */
public slots:
    void clickSreachData();
};

QManagerWidgetPrivate::QManagerWidgetPrivate(QManagerWidget *q)
    : q_ptr(q)
    , tableAttribute(nullptr)
    , tableWidget(new QTableWidget(q))
    , btnSearch(new QPushButton("查询"))
    , m_btnQuery(new QPushButton(QStringLiteral("查  询")))
    , m_btnPrePage(new QPushButton(QStringLiteral("上一页")))
    , m_btnNextPage(new QPushButton(QStringLiteral("下一页")))
    , m_btnFristPage(new QPushButton(QStringLiteral("首  页")))
    , m_btnEndPage(new QPushButton(QStringLiteral("尾  页")))
    , m_edtPage(new QSpinBox())
    , m_labTotalPage(new QLabel())
    , m_btnJmpPage(new QPushButton(QStringLiteral("跳 转")))
{

}
void QManagerWidgetPrivate::initUI()
{
    Q_Q(QManagerWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout();
    for(auto item : tableAttribute->fieldAttributes){
         QWidget *inputWidget = InputWidgetUtils::createSreachWidget(item);
        if(nullptr == inputWidget){
             continue;
        }
        QLabel * lab = new QLabel(item.label);
        inputWidgets.push_back(inputWidget);
        QHBoxLayout *edtLay = new QHBoxLayout();
        edtLay->addWidget(lab);
        edtLay->addWidget(inputWidget);
        mainLayout->addLayout(edtLay);
    }
    mainLayout->addWidget(btnSearch);
    mainLayout->addWidget(tableWidget);

    QHBoxLayout *m_pageLayout= new QHBoxLayout;
    m_pageLayout->addWidget(m_btnFristPage);
    m_pageLayout->addWidget(m_btnPrePage);
    m_pageLayout->addWidget(m_btnNextPage);
    m_pageLayout->addWidget(m_btnEndPage);
    m_pageLayout->addWidget(m_edtPage);
    m_pageLayout->addWidget(m_labTotalPage);
    m_pageLayout->addWidget(m_btnJmpPage);

    connect(btnSearch,&QPushButton::clicked,this,&QManagerWidgetPrivate::clickSreachData);
    mainLayout->addLayout(m_pageLayout);
    q->setLayout(mainLayout);
}
bool QManagerWidgetPrivate::initData(QSqlDatabase *_db, QTableAttribute *_tableAttribute)
{
    db = _db;
    tableAttribute = _tableAttribute;
    return true;
}

inline void QManagerWidgetPrivate::updateData()
{
    QString sql = "select * from "+ tableAttribute->tableName+";";
    QSqlQuery query(sql,*db);
    query.exec();
    DynamicFormDataList datalist;
    while(query.next()){
        DynamicFormDataItem cell;
        QList<DynamicFormDataItem> row;
        for(auto item : tableAttribute->fieldAttributes){
            int fieldNo = query.record().indexOf(item.fieldName);
            QString value = query.value(fieldNo).toString();
            cell.key = item.fieldName;
            cell.value = value;
            row.push_back(cell);
        }
        datalist.datas.push_back(row);
    }
    tableWidget->clearContents();
    tableWidget->setRowCount(datalist.datas.size());
    for(int i = 0 ; i < datalist.datas.size();i++){
        auto row = datalist.datas.at(i);
        int col = 0;
        tableWidget->setColumnCount(row.size());
        for(auto item : row){
            qInfo()<< i <<":"<< col <<":"<<item.value;
            QTableWidgetItem *itemWidget = new QTableWidgetItem(item.value);
            tableWidget->setItem(i,col,itemWidget);
            col++;
        }
    }
}

inline void QManagerWidgetPrivate::clickSreachData()
{
    maps.clear();
    // 获取查询的内容
    for(auto item : inputWidgets){
        QString value = InputWidgetUtils::getInputWidgetValue(item);
        if(value.isEmpty()){
            continue;
        }
        maps.insert(item->property("name").toString(),value);
    }
    updateData();
}


#endif // QMANAGERWIDGET_P_H
